package com.xm.security.core.impl;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.security.DigestInputStream;
import java.security.MessageDigest;
import java.util.Base64;

/**
 * 描述: Function: 消息摘要处理实现类
 *
 * @author chen-guipeng
 * @date 2022/8/3
 */
public class DigestImpl {

    /**
     * 算法名称
     */
    private String algorithm;

    public DigestImpl(String algorithm) {
        this.algorithm = algorithm;
    }

    /**
     * 提取摘要
     *
     * @param data     待提取摘要数据
     * @param password 密码
     * @return
     * @throws Exception
     */
    public byte[] encode(byte[] data, byte[] password) throws Exception {
        MessageDigest md = MessageDigest.getInstance(this.algorithm);
        md.update(password);

        return md.digest(data);
    }

    /**
     * 提取摘要
     *
     * @param data 待提取摘要数据
     * @return
     * @throws Exception
     */
    public byte[] encode(byte[] data) throws Exception {
        return MessageDigest.getInstance(this.algorithm).digest(data);
    }

    /**
     * 消息摘要
     *
     * @param data 待摘要数据
     * @return
     */
    public byte[] encode(String data) throws Exception {
        return encode(data.getBytes(StandardCharsets.UTF_8));
    }

    /**
     * 文件摘要
     *
     * @param file 待摘要的文件
     * @return
     * @throws Exception
     */
    public byte[] encode(File file) throws Exception {
        DigestInputStream dis = null;
        try {
            dis = new DigestInputStream(new FileInputStream(file), MessageDigest.getInstance(this.algorithm));
            int size = 1024;
            byte[] buf = new byte[size];
            while (true) {
                if (dis.read(buf, 0, size) < 0) {
                    break;
                }
            }
            return dis.getMessageDigest().digest();
        } finally {
            if (dis != null) {
                try {
                    dis.close();
                } catch (IOException e) {
                }
                dis = null;
            }
        }
    }

    /**
     * 提取摘要
     *
     * @param data
     * @return
     * @throws Exception
     */
    public String encodeString(byte[] data, byte[] password) throws Exception {
        return Base64.getEncoder().encodeToString(encode(data, password));
    }

    /**
     * 提取摘要
     *
     * @param data
     * @return
     * @throws Exception
     */
    public String encodeString(byte[] data) throws Exception {
        return Base64.getEncoder().encodeToString(encode(data));
    }

    /**
     * 提取摘要
     *
     * @param data
     * @return
     */
    public String encodeString(String data) throws Exception {
        return encodeString(data.getBytes(StandardCharsets.UTF_8));
    }

    /**
     * 提取摘要
     *
     * @param data
     * @return
     */
    public String encodeString(String data, String password) throws Exception {
        return encodeString(data.getBytes(StandardCharsets.UTF_8), password.getBytes(StandardCharsets.UTF_8));
    }

    /**
     * 文件摘要
     *
     * @param file 待摘要的文件
     * @return
     * @throws Exception
     */
    public String encodeString(File file) throws Exception {
        // return Base64.getEncoder().encodeToString(encode(file));
        return byteArrayToHex(encode(file));
    }

    /**
     * byte[] 转 String
     *
     * @param bytes
     * @return String
     */
    public String byteArrayToHex(byte[] bytes) {
        String hs = "";
        String stmp = "";
        for (int n = 0; n < bytes.length; n++) {
            stmp = (Integer.toHexString(bytes[n] & 0XFF));
            if (stmp.length() == 1) {
                hs = hs + "0" + stmp;
            } else {
                hs = hs + stmp;
            }
            if (n < bytes.length - 1) {
                hs = hs + "";
            }
        }
        return hs;
    }

}
